package com.supermali.entity.map.hinder;

import com.supermali.behavior.move.MoveBehavior;
import com.supermali.behavior.up_down.UpDownBehavior;
import com.supermali.behavior.up_down.hinder.HinderUpDown;
import com.supermali.creater.MapCreater;
import com.supermali.creater.img.ImgHelper;
import com.supermali.creater.img.ImgKey;
import com.supermali.creater.img.ImgLoader;
import com.supermali.entity.MapImageAbstract;
import com.supermali.entity.map.background.dynamic.BreakBrick;
import com.supermali.entity.map.background.dynamic.DynamicBackGroundMapAbstract;
import com.supermali.entity.npc.person.Person;

import java.awt.image.BufferedImage;
import java.util.List;

/**
 * @project super-mali
 * @Date 2021/2/28
 * @Auth yangrui
 **/
public class RedBrick extends HinderMapAbstract {

    private UpDownBehavior upDownBehavior;

    public RedBrick(Double x, Double y, MapCreater mapCreater) {
        super(x, y, mapCreater);
        upDownBehavior = new HinderUpDown(this);
    }

    @Override
    public void init() {
        ImgHelper imgHelper = ImgLoader.getImgHelper(ImgKey.Hinder.RED_BRICK);
        BufferedImage select = imgHelper.select(0);
        this.setBufferedImage(select);
    }

    @Override
    public void proccessData(long delta) {
        super.proccessData(delta);
        // 向上后向下移动
        if(!upDownBehavior.isOver()){
            upDownBehavior.upDown(delta);
        }
    }

    @Override
    public void destroy(long delta) {
//        super.destroy(delta);
        Person person = getMapCreater().getPerson();
        if(person.getBigStatus()== Person.BigStatus.SMALL) {
            upDownBehavior.start();
        }else {
            List<MapImageAbstract> removes = getMapCreater().getRemoves();
            removes.add(this);
            List<DynamicBackGroundMapAbstract> dynamicBackgroundAbstracts = getMapCreater().getDynamicBackgroundAbstracts();

            int width = this.getBufferedImage().getWidth();
            int height = this.getBufferedImage().getHeight();
            createBreakBrick(dynamicBackgroundAbstracts,this.getX(),this.getY(),-1,30);
            createBreakBrick(dynamicBackgroundAbstracts,this.getX(),this.getY()+height/2,-1,35);
            createBreakBrick(dynamicBackgroundAbstracts,this.getX()+width/2,this.getY()+height/2,1,35);
            createBreakBrick(dynamicBackgroundAbstracts,this.getX()+width/2,this.getY(),1,30);
        }
    }

    private void createBreakBrick(List<DynamicBackGroundMapAbstract> dynamicBackgroundAbstracts, double x,double y,int direct,double vy){
        BreakBrick breakBrick = new BreakBrick(x, y, getMapCreater());
        dynamicBackgroundAbstracts.add(breakBrick);
        breakBrick.getJumpBehavior().setVy(vy);
        breakBrick.getJumpBehavior().start();
        MoveBehavior moveBehavior = breakBrick.getMoveBehavior();
        moveBehavior.setV(200d/3000);
        moveBehavior.setXdirect(direct);
        moveBehavior.start();
    }
}
